package com.sdsy.constant;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.sdsy.config.IntegrationTestConfig;
import com.sdsy.config.TestnetConfig;
import com.sdsy.util.RecreationChainUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Contract;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by shao on 2018/4/13.
 */
public class WalletConfig {
    private Logger logger = LoggerFactory.getLogger(WalletConfig.class);

    public static Map<String, Object> map = new HashMap<String, Object>();
    @Autowired
    private Config config;

    private IntegrationTestConfig config_ = new TestnetConfig();

    /**
     * 实例化合约对象
     */
    public void initContract() {
        System.out.println("initContract=================");
//        long startTime = System.currentTimeMillis();
//        logger.info("实例化合约对象开始=====================");
//        // 创建Web3J
//        Web3j web3 = Web3j.build(new HttpService("https://mainnet.infura.io/c8LMJFLLL7rUEBtxozvJ"));
////
//        // 加载钱包
//        Credentials credentials = null;
//        try {
//            // 构建
//            byte[] key = Base64Utils.decodeFromString(config.getRandom());
//            AES aes = SecureUtil.aes(key);
//            String decryptStr = aes.decryptStr(config.getPassword(), CharsetUtil.CHARSET_UTF_8);
//            credentials = WalletUtils.loadCredentials(decryptStr, new File(config.getAccount_path()));
//        } catch (CipherException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        //加载合约
//        BigInteger GAS = Contract.GAS_LIMIT;
//        BigInteger GAS_PRICE = Contract.GAS_PRICE;
//        RecreationChainUtil keySCode = RecreationChainUtil.load(config.getSmart_contract_address(), web3, credentials, GAS_PRICE, GAS);
//        logger.info("实例化合约对象结束=====================");
//        long endTime = System.currentTimeMillis();
//        System.out.println("实例化合约对象耗时 : " + (endTime - startTime) / 1000);
//        map.put("keySCode", keySCode);

    }

    public static Map<String, Object> getMap() {
        return map;
    }


}
